<extend name="Layout:layout_content" />
<block name="head_after">
    <style type="text/css">
        .summary { margin: 10px;}
        .summary span { margin-right:5px;}
        .xsui-form-field { width:230px; float:left;}
        .xsui-form-field label { font-weight:normal; width:60px; display:inline-block;}
    </style>
</block>
<block name="body_main">
    <ul class="xsui-breadcrumb">
        <li><a href="{:AddonU('customer_list')}"><i  class="fa fa-home fa-fw"></i>客户管理</a></li>
        <li class="current">编辑客户</li>
    </ul>
    <h3 class="htitle">{$meta_title}</h3>
    <div class="toolbar-buttons">
        <if condition="false">
            <a href="{:U('Order/order_add','buyerID='.$data['id'])}" class="xsui-btn ui-state-default">新增订单</a><a href="{:U('Order/customer_order_list','buyerID='.$data['id'])}" class="xsui-btn ui-state-default" data-window-name="orderList">查看订单</a><a href="{:U('CallRecord/callrecord_of_customer_list','tel='.$data['telphone'])}" class="xsui-btn ui-state-default" data-window-name="cdr">通话记录</a>
        </if>
    </div>
    <div class="summary">
        <span>来电记录：
            <select id="exten-callrecord-topn" data-screenpop-url="{:AddonU('customer_edit','tel=vartel')}" data-source-url="{:AddonU('ApiCallRecord/exten_get_callrecord_list','limit=10&exten='.$login_data['user_name'])}"><option value="">==最近通话记录==</option>
                <!--<option>13950076987 于16:15:12</option><option>13950070000 于16:10:32</option><option>13950070000 于16:10:32</option><option>13950070000 于16:10:32</option>-->
            </select>
        </span>
        <if condition="false">
            <span>消费次数：<strong>{$data.buy_count}</strong></span>
            <span>累计消费：<strong>{$data.total_amount}</strong></span>
            <span>上次购买：<strong>{$data.buy_time}</strong></span>
        </if>
    </div>
    <form action="{:AddonU('customer_edit_save')}" method="post" class="xsui-form" enctype="multipart/form-data">
        <fieldset class="xsui-switch-fieldset">
            <legend><a href="#" class="xsui-switch">[-]</a>基础信息</legend>            
            <div style="max-height:300px; overflow-y:auto; width:540px;" class="xsui-switch-content">
                <div class="xsui-form-field">
                    <label>客户编号：</label>
                    <input type="text" name="serial_no" placeholder="客户编号" value="{$data.serial_no}" required="required" />
                </div>
                <div class="xsui-form-field">
                    <label>客户姓名：</label>
                    <input type="text" name="name" value="{$data.name}" placeholder="姓名"  required="required" />
                </div>
                <div class="xsui-form-field">
                    <label>联系电话：</label>
                    <input type="text" name="telphone" value="{$data.telphone}" placeholder="联系电话" data-check-url="{:AddonU('check_telphone')}" required="required" />
                    <span id="number-regional" data-url="{:U('NumberRegional/get_regional','addon=POrgCommonService&tel=valtel')}" style="color:#F00;">(号码归属地)</span>
                    <a href="#" id="number-regional-refresh" title="刷新号码归属地"><i class="fa fa-refresh" style="color:#09C"></i></a>
                </div>
                <div class="xsui-form-field">
                    <label>客户性别：</label>
                    <select name="sex">{:parse_htmlselect_options('男;女;未知',$data['sex'])}</select>
                </div>
                <div class="xsui-form-field">
                    <label>联系地址：</label>
                    <input type="text" name="address" size="52" placeholder="收货地址,如：XX省 XX市 XX区 XX路XXX号XX幢附X楼" value="{$data.address}" />
                    <if condition="$data['id']">
                        <a id="addr-mgr" href="{:AddonU('CustomerAddress/address_picker','customer_id='.$data['id'])}">[管理]</a>
                    </if>
                    <label>&nbsp;</label>
                    <sohtml:region names="province,city,area" nameattr="id" changeFunc="changeRegion" />
                </div>
                <div class="xsui-form-field">
                    <label>身份证号：</label>
                    <input type="text" name="id_no" placeholder="身份证号码" value="{$data.id_no}"/>
                </div>
                <div class="xsui-form-field">
                    <label>邮政编码：</label>
                    <input type="text" name="zip" placeholder="邮政编码" value="{$data.zip}"/>
                </div>
                <div class="xsui-clear"></div>
            </div>
        </fieldset>
        <fieldset class="xsui-switch-fieldset">
            <legend><a href="#" class="xsui-switch">[-]</a>扩展信息</legend>            
            <div style="max-height:300px; overflow-y:auto; width:540px;" class="xsui-switch-content">
                <foreach name="fields" item="vo">                    
                    <div class="xsui-form-field">
                        <label>{$vo.title}：</label>
                        <switch name="vo['options_input_type']">
                        <case value="text">
                            <input type="text" name="{$vo.field_name}" value="{$data[$vo['field_name']]}" placeholder="{$vo.title}" class="$vo.class" />
                        </case>
                        <case value="select">                            
                            <select>{:parse_htmlselect_options($vo['options_content'])}</select>
                        </case>
                        <case value="radio">
                            <input type="radio" />
                        </case>
                        <case value="checkbox">
                            <input type="checkbox" />
                        </case>
                        <case value="textarea">
                            <textarea name="" cols="51" rows="3"></textarea>
                        </case>
                        <case value="editor">
                            <textarea name="" cols="51" rows="3" class="editor"></textarea>
                        </case>
                        <case value="file">
                            <input type="file" name="{$vo.field_name}" placeholder="{$vo.title}" />
                        </case>
                        <default />                    
                        <input type="text" name="{$vo.field_name}" value="{$data[$vo['field_name']]}" placeholder="{$vo.title}"{$vo.class} />
                        </switch>
                    </div>
                </foreach>
                <div class="xsui-clear"></div>
            </div>
        </fieldset>
        <button type="submit" class="ui-state-default"><i class="fa fa-save"></i>保存</button>
        <button type="reset" class="ui-state-default">重置</button>
        <input type="hidden" name="id" value="{$data.id}" />
    </form>
    <input type="hidden" id="hidval_region_api" value="{:U('Area/index','addon=POrgCommonService')}" />
</block>
<block name="body_after">
    <script src="__THEME__Plugin/jquery-cnregion-1.0/jquery-cnregion-1.0.js" type="text/javascript"></script>
    <script type="text/javascript">
        // 布局修正
        $(':file, [name="telphone"],[name="address"], [name="remark"], textarea').parent().width('480px');
        /**
         * 地区自动输入
         * @param {string} p 省份
         * @param {string} c 城市
         * @param {string} a 地区
         * @returns {undefined}
         * @since 1.0 2014-12-11 by sutroon
         * @remark 在文本框中加英文逗号,逗号后面的字串将被保留,如：福建省 厦门市 思明区, 湖滨南路(注意逗号位置)
         */
        function changeRegion(p, c, a) {
            var val = p;
            if (c) {
                val = val + ' ' + c;
            }
            if (a) {
                val = val + ' ' + a;
            }
            var $tb = $('input[name="address"]');
            var oldv = $tb.val();
            var pos = oldv.lastIndexOf(' ');
            if (pos > -1) {
                val += oldv.substring(pos);
            }
            $tb.val(val);
        }
        // 地址选择对话框
        $('#addr-mgr').click(function () {
            var opts = {'title': '地址管理', 'width': 580, 'close': function () {
                    $(this).remove()
                }};
            $('<div><iframe name="addrmgr" style="width:100%; overflow:hidden;" frameborder="0" scrolling="no" src="' + $(this).attr('href') + '"></iframe></div>').dialog(opts).find('iframe').load(function () {
                var $ifrm = $(this);
                var height = $ifrm.contents().find("body").height();
                $ifrm.height(height).parent().height(height + 10);
            });
            return false;
        })
        // 选择地址 2014-12-15 by sutroon
        function set_address(name, telphone, address, zip) {
            $('input[name="name"]').val(name);
            $('input[name="telphone"]').val(telphone);
            $('input[name="address"]').val(address);
            $('input[name="zip"]').val(zip);
        }
        exten_get_callrecord_list();
        /**
         * 获取分机最近10条通话记录
         * @returns {undefined}
         * @since 1.0 2016-12-16 by sutroon
         */
        function exten_get_callrecord_list() {
            var url = $('#exten-callrecord-topn').data('sourceUrl');
            $.ajax({
                url: url,
                dataType: "jsonp",
                jsonp: "exten_get_callrecord_list_jsonp"
            })
        }
        // 获取历史通话记录 2014-12-16 by sutroon
        function exten_get_callrecord_list_jsonp(data) {
            var $topn = $('#exten-callrecord-topn');
            var url = $topn.data('screenpopUrl');
            var str = '<option value="">==最近通话记录==</option>';
            for (var i in data) {
                str += '<option value="' + data[i].line + '">' + data[i].line + ' 于' + data[i].create_time.substring(11) + '</option>';
            }
            $topn.html(str);
        }
        // 通话历史下拉框点击的时候跳转 2016-2-1 by sutroon
        $('#exten-callrecord-topn').change(function () {
            var val = $(this).find(':selected').val();
            if (!val) {
                return;
            }
            if (!confirm('您确定要转到[' + val + ']的客户资料吗?')) {
                return;
            }
            var url = $(this).data('screenpopUrl');
            location.href = url.replace('vartel', val);
        })
        // 检测电话是否重复
        $('input[name="telphone"]').blur(function () {
            var $this = $(this);
            if (!$this.val()) {
                return;
            }
            $.get($this.data('checkUrl'), {'tel': $this.val()}, function (data) {
                if (data.success != '1') {
                    $.somessager.alert('提示', '电话号码已存在!');
                    $this.val('');
                }
            })
        })
        // 加载归属地 2015-1-5 by sutroon
        if ($('input[name="telphone"]').val()) {
            var url = $('#number-regional').data('url').replace('valtel', $('input[name="telphone"]').val());
            $('#number-regional').load(url);
        }
        // 刷新归属地 2016-1-23 by SoChishun
        $('#number-regional-refresh').click(function () {
            var url = $('#number-regional').data('url').replace('valtel', $('input[name="telphone"]').val());
            $('#number-regional').load(url);
            return false;
        })
    </script>
</block>